package pages

import (
	"github.com/GoAdminGroup/go-admin/context"
	"github.com/GoAdminGroup/go-admin/modules/db"
	"github.com/GoAdminGroup/go-admin/plugins/admin/modules/table"
	"github.com/GoAdminGroup/go-admin/template/types"
	"github.com/GoAdminGroup/go-admin/template/types/form"
	"newclub/services"
)

func GetActivitiesTable(ctx *context.Context) table.Table {

	activities := table.NewDefaultTable(ctx, table.DefaultConfigWithDriver("mysql"))

	info := activities.GetInfo().SetFilterFormLayout(form.LayoutFilter)

	info.AddField("Id", "id", db.Int)
	info.AddField("社团名称", "name", db.Int).FieldJoin(types.Join{
		Table:     "clubs",   // 连表的表名
		Field:     "club_id", // 要连表的字段
		JoinField: "id",      // 连表的表的字段
	}).FieldDisplay(func(value types.FieldModel) interface{} {
		return value.Row["clubs_goadmin_join_name"]
	})
	info.AddField("活动标题", "title", db.Varchar).
		FieldFilterable()
	info.AddField("开始时间", "start_date", db.Timestamp)
	info.AddField("结束时间", "end_date", db.Timestamp)
	info.AddField("创建时间", "created_at", db.Timestamp)

	info.SetTable("activities").SetTitle("活动管理").SetDescription("活动管理")

	detail := activities.GetDetailFromInfo()
	detail.AddField("描述", "description", db.Text)

	formList := activities.GetForm()
	formList.AddField("Id", "id", db.Int, form.Default).
		FieldDisableWhenCreate()
	formList.AddField("活动标题", "title", db.Varchar, form.Text)
	formList.AddField("社团名称", "club_id", db.Int, form.SelectSingle).FieldOptions(services.TransFieldOptions(services.GetClubs(), "name", "id")).FieldMust()
	formList.AddField("描述信息", "description", db.Text, form.RichText)
	formList.AddField("开始时间", "start_date", db.Timestamp, form.Datetime)
	formList.AddField("结束时间", "end_date", db.Timestamp, form.Datetime)
	formList.AddField("创建时间", "created_at", db.Timestamp, form.Datetime).
		FieldHide().FieldNowWhenInsert()

	formList.SetTable("activities").SetTitle("活动管理").SetDescription("活动管理")

	return activities
}
